package org.apereo.cas.services;

import lombok.extern.slf4j.Slf4j;
import org.apereo.cas.authentication.principal.Service;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

/**
 * @author automannn
 * @Date 2022/11/21
 * @Description 该类被高版本(> 4.2.7)删除，此处为了学习，将其搬过来
 */
@Entity
@DiscriminatorValue("ant")
@Slf4j
public class RegisteredServiceImpl extends AbstractRegisteredService {

    private static final PathMatcher PATH_MATCHER = new AntPathMatcher();

    public RegisteredServiceImpl() {
        super();
        log.info("[{}] is deprecated and will be removed in future CAS releases. Consider using [{}] instead.",
                this.getClass().getSimpleName(), RegexRegisteredService.class.getSimpleName());
    }

    @Override
    public void setServiceId(final String id) {
        this.serviceId = id;
    }

    @Override
    public boolean matches(final Service service) {
        return service != null && PATH_MATCHER.match(serviceId.toLowerCase(), service.getId().toLowerCase());
    }

    @Override
    public boolean matches(String serviceId) {
        return serviceId != null && PATH_MATCHER.match(serviceId.toLowerCase(), serviceId.toLowerCase());
    }

    @Override
    protected AbstractRegisteredService newInstance() {
        return new RegisteredServiceImpl();
    }
}
